package logic

import (
	"context"
	"github.com/zeromicro/go-zero/core/utils"
	"time"
	"zero-cloud-disk/api/models"

	"zero-cloud-disk/api/internal/svc"
	"zero-cloud-disk/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type FileUploadLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewFileUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FileUploadLogic {
	return &FileUploadLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *FileUploadLogic) FileUpload(req *types.FileUploadReq) (resp *types.FileUploadRes, err error) {
	rp := &models.RepositoryPool{
		Identity:  utils.NewUuid(),
		Hash:      req.Hash,
		Name:      req.Name,
		Ext:       req.Ext,
		Size:      req.Size,
		Path:      req.Path,
		CreatedAt: time.Time{},
		UpdatedAt: time.Time{},
		DeletedAt: time.Time{},
	}
	_, err = l.svcCtx.Engine.Insert(rp)
	if err != nil {
		return nil, err
	}
	return &types.FileUploadRes{Identity: rp.Identity, Ext: rp.Ext, Name: rp.Name}, nil
}
